package com.swjd.type;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

//批量发送消息
public class ProducerBatch {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //消息发送者（生产着)
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //发送给服务器地址
        producer.setNamesrvAddr("localhost:9876");
        //启动服务
        producer.start();
        //创建要发送的消息·
        int i = 0;
        String msg1 = ("这是第" + i++ + "条消息");
        Message message1 = new Message("topic3", "tag1", msg1.getBytes());
        String msg2 = ("这是第" + i++ + "条消息");
        Message message2 = new Message("topic3", "tag1", msg2.getBytes());
        String msg3 = ("这是第" + i++ + "条消息");
        Message message3 = new Message("topic3", "tag1", msg3.getBytes());
        String msg4 = ("这是第" + i++ + "条消息");
        Message message4 = new Message("topic3", "tag1", msg4.getBytes());
        String msg5 = ("这是第" + i++ + "条消息");
        Message message5 = new Message("topic3", "tag1", msg5.getBytes());
        List<Message> msglist = new ArrayList<>();
        msglist.add(message1);
        msglist.add(message2);
        msglist.add(message3);
        msglist.add(message4);
        msglist.add(message5);
        SendResult sendResult = producer.send(msglist);
        System.out.println(sendResult);
        producer.shutdown();
    }
}
